#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minSpeedOnTime(vector<int>& dist, double hour) {
        int left = 1, right = 1e7 + 10;
        double cost = 0, n = dist.size();
        while (left <= right)
        {
            cost = 0;
            int mid = left + (right - left) / 2;
            for (int i = 0; i < n; i++)
            {
                if (i == n - 1) cost += (double)dist[i] / mid;
                else if (i < n - 1 && dist[i] % mid == 0) cost += dist[i] / mid;
                else cost += (dist[i] / mid + 1);
            }
            if (cost > hour) left = mid + 1;
            else if (cost < hour) right = mid - 1;
            else break;
        }
        if (left > 1e7 + 10) return -1;
        return left;
    }
};